package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import com.ruoyi.wms.dto.InventoryItemDto;
import com.ruoyi.wms.vo.InventoryItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wms.domain.InventoryItem;
import com.ruoyi.wms.service.IInventoryItemService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 盘点明细Controller
 *
 * @author ruoyi
 * @date 2025-11-19
 */
@Api(value = "盘点明细Controller", tags = "盘点明细Controller")
@RestController
@RequestMapping("/stockInventoryItem")
public class InventoryItemController extends BaseController {
    @Autowired
    private IInventoryItemService inventoryItemService;

    /**
     * 查询盘点明细列表
     */
    @RequiresPermissions("wms:InventoryItem:list")
    @GetMapping("/list")
    public TableDataInfo list(InventoryItemDto inventoryItem) {
        startPage();
        List<InventoryItemVo> list = inventoryItemService.selectInventoryItemList(inventoryItem);
        return getDataTable(list);
    }


    /**
     * 获取盘点明细详细信息
     */
    @RequiresPermissions("wms:InventoryItem:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(inventoryItemService.selectInventoryItemById(id));
    }


    /**
     * 修改盘点明细
     */
    @RequiresPermissions("wms:InventoryItem:edit")
    @Log(title = "盘点明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InventoryItem inventoryItem) {
        return toAjax(inventoryItemService.updateInventoryItem(inventoryItem));
    }

    /**
     * 删除盘点明细
     */
    @RequiresPermissions("wms:InventoryItem:remove")
    @Log(title = "盘点明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(inventoryItemService.deleteInventoryItemByIds(ids));
    }


    @RequiresPermissions("wms:InventoryItem:query")
    @ApiOperation("查询 盘点明细 ,再修改盘点明细 ")
    @GetMapping(value = "/getInfoInventoryItemInfo")
    public AjaxResult getInfoInventoryItemInfo(@Validated  InventoryItemDto dto
    ) {
        return success(inventoryItemService.getInfoInventoryItemInfo(dto));
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody InventoryItem inventoryItem) {
        try {
            return success(inventoryItemService.save(inventoryItem));
        }catch (Exception e){
            e.printStackTrace();
            return  error("调用失败");
        }
    }

    @ApiOperation("查询 盘点结果")
    @GetMapping(value = "/queryInventoryItemResult")
    public AjaxResult queryInventoryItemResult(@RequestParam @Validated @NotBlank(message = "批次号不能为空") String batchNum
    ) {
        return success(inventoryItemService.queryInventoryItemResult(batchNum));
    }

    @ApiOperation("查询 未盘点数量 ")
    @GetMapping(value = "/queryNotInventoryItemCnt")
    public AjaxResult queryNotInventoryItemCnt(@RequestParam @Validated @NotBlank(message = "批次号不能为空") String batchNum
    ) {
        return success(inventoryItemService.queryNotInventoryItemCnt(batchNum));
    }
}